import Vue from 'vue'
import router from './router'
import axios from 'axios'
import JSONBig from 'json-bigint'
axios.defaults.baseURL = 'http://ttapi.research.itcast.cn/mp/v1_0/'

axios.interceptors.request.use(function (config) {
  var token = window.sessionStorage.getItem('token')
  if (token) {
    config.headers.Authorization = 'Bearer ' + token
  }
  return config
},
function (error) {
  return Promise.reject(error)
})
axios.interceptors.response.use(function (response) {
  // axios完整请求的后续逻辑处理
  return response
}, function (error) {
  // Do something with response error
  if (error.response.status === 401) {
    router.push({ name: 'login' })
  }
  return Promise.reject(error)
})
axios.defaults.transformResponse = [function (data) {
  if (data) {
    return JSONBig.parse(data)
  } else {
    return data
  }
}]
Vue.prototype.$http = axios
